<template>
  <h2>toRaw 与 markRaw</h2>
  <hr />
  <h3>state:{{ state }}</h3>
  <button @click="testToRaw">测试ToRaw</button>
  <button @click="testMarkRaw">测试MarkRaw</button>
</template>
<script lang="ts">
interface UserInfo {
  name: string;
  age: number;
  // ?问号表示 这个变量可能不存在
  likes?: string[];
}
import { defineComponent, markRaw, reactive, toRaw } from "vue";
export default defineComponent({
  name: "App",
  setup() {
    let state = reactive<UserInfo>({
      name: "刘学奇",
      age: 10,
    });

    const testToRaw = () => {
      // 将代理对象变成普通对象，数据变化，界面不变化
      const user = toRaw(state);
      user.name += "===";
      console.log(user);
    };

    const testMarkRaw = () => {
      // 标记一个对象，使其永远不会转换为代理。返回对象本身
      const likes = ["跑步", "做崔子"];
      state.likes = markRaw(likes);
      // 没有渲染之前可以改变的，渲染之后无法改变
      state.likes[0] += "--";
      setTimeout(() => {
        if (state.likes) {
          state.likes[0] += "--";
        }
      }, 1000);
    };
    return { state, testToRaw, testMarkRaw };
  },
});
</script>

<style>
</style>
